import groovy.ui.SystemOutputInterceptor
import com.kdayun.z1.core.context.Context
import com.kdayun.z1.core.util.SqlExecutor
import com.kdayun.z1.core.util.FileUitls
import com.kdayun.z1.core.message.MessageUtil

import org.springframework.util.StringUtils

import com.kdayun.manager.common.CoreMsgUpdate
/**
 * 5.0.2版本更新的脚本文件
 * 删除了一些冗余的表
 * */
public void start() {
	SqlExecutor sqlExecutor=Context.getInstance().getBean("sqlExecutor")
	
	if(sqlExecutor==null) {
		throw new Exception("更新失败,无法获取sqlExecutor类的bean实例");
	}
	def sqls="""UPDATE core_model_design SET MODELD_VALUE = replace(MODELD_VALUE, 'zheling', 'kdayun') ;
UPDATE core_dictionarymx SET ZDZ_ZIDZ = replace(ZDZ_ZIDZ, 'zheling', 'kdayun') ;
UPDATE core_plan SET BEANCLASS = replace(BEANCLASS, 'zheling', 'kdayun') ;
UPDATE core_plugin SET TUPDZ = replace(TUPDZ, 'zheling', 'kdayun'),CHAJLJ = replace(CHAJLJ, 'zheling', 'kdayun'),XINXZLJ = replace(XINXZLJ, 'zheling', 'kdayun'),SHEZLJ = replace(SHEZLJ, 'zheling', 'kdayun')  ;
UPDATE core_res SET RES_ACTION = replace(RES_ACTION, 'zheling', 'kdayun') ;
UPDATE act_ge_bytearray SET BYTES_ = replace(BYTES_, 'zheling', 'kdayun') ;
UPDATE core_sysopt SET XTYPE = replace(XTYPE, 'zheling', 'kdayun') ;
UPDATE bd_org SET JIANC='所有机构' ,OBJNAME='所有机构' where RWID='9C876276D8884BFF94DF343E2A4EED7D';
UPDATE core_res SET OBJNAME='机构管理' where RWID='698431E0B2A049169864086DE67DAF1D';
UPDATE core_res SET OBJNAME='机构管理' where RWID='73EBEC07002340DBB927C9BF9F45ECEA';
UPDATE core_filesres SET WJZY_MINGC='机构管理' where RWID='02763F4988DA42B082FFB019DD584CCA';
UPDATE core_filesres set WJZY_ZIYLJ=replace(WJZY_ZIYLJ, '/filesres/', '/static/filesres/') WHERE WJZY_ZIYLJ   LIKE '/filesres/%';
UPDATE core_sysopt set  V = replace(V, '/filesres/', '/static/filesres/') WHERE  V LIKE '%filesres/%' AND V NOT LIKE '%/static/filesres/%';
UPDATE core_sysoptmx set  V = replace(V, '/filesres/', '/static/filesres/') WHERE     V LIKE '%filesres/%';
UPDATE core_user set  TOUX = replace(TOUX, '/filesres/', '/static/filesres/') WHERE	TOUX LIKE '/filesres/%';
UPDATE core_model_design set MODELD_VALUE=replace(MODELD_VALUE, '/filesres/', '/static/filesres/') WHERE MODELD_VALUE LIKE '%/filesres/%'; 
delete  FROM core_tables_columns a where a.MXZD_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_event');
delete  FROM core_tables_indexs a where a.MXSY_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_event');
delete  FROM core_tables_links a where a.MXYS_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_event');
delete  FROM core_tables_columns a where a.MXZD_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_default');
delete  FROM core_tables_indexs a where a.MXSY_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_default');
delete  FROM core_tables_links a where a.MXYS_TABLEID IN ( SELECT RWID from core_tables where MX_BIANH='core_model_default');
DROP TABLE IF EXISTS `core_model_event`;
DROP TABLE IF EXISTS `core_model_default`;"""	
	
	String[] sqlarry=sqls.split(";");
	System.out.println(sqlarry)
	for(int i=0;i<sqlarry.length;i++) {
		sqlExecutor.execSqlBatch(sqlarry[i]);
	}
	
	Context.getInstance().logService.info("execSqlBatch:"+sqls)
	
	def delFile=[
		"static/framework/dbtable.js",
		"static/framework/echarts.1.js",
		"static/framework/echarts.js",
		"static/framework/echartsTheme.js",
		"static/framework/echartTooltipExt.js",
		"static/framework/editform.json",
		"static/framework/PageForm.json",
		"static/framework/zlDataSet.js",
		"static/framework/zlDigitalCardFlipper.js",
		"static/framework/zlEditform.js",
		"static/framework/zlFrame.js",
		"static/framework/zlGridEditForm.js",
		"static/framework/zlGridPage.js",
		"static/framework/zlImgList.js",
		"static/framework/zlRollTable.js",
		"static/appdata/filesres/excelTemplate/BsBudChengzjxx.xls",
		"static/appdata/filesres/excelTemplate/BsBudFangwdj.xls",
		"static/appdata/filesres/excelTemplate/BsBudFangwdjDaor.xls",
		"static/appdata/filesres/excelTemplate/BsBudShebxx.xls",
		"static/appdata/filesres/excelTemplate/BsBudShebxxdj.xls",
		"static/image/adoutbc.png",
		"static/image/China.jpg",
		"static/image/datasource.png",
		"static/image/home.png",
		"static/image/page.png",
		"static/image/phone.png",
		"static/image/shadow-left.png",
		"static/image/shadow-right.png",
		"static/image/version.png",
		"static/image/video.png",
		"static/image/wechat.png",
		"static/image/zlwc.png",
		"static/image/鍙橀噺.png",
		"static/libs/formdesign/libs/ztree/js/jquery.ztree.all.min11.js",
		"static/libs/formdesign/static/image/bigscreen/earth-rotate.d4a0f112.gif",
		"static/libs/formdesign/static/image/blue/dark",
		"static/libs/layui/lay/modules/table - 鍓湰 (2).js",
		"static/libs/layui/lay/modules/table - 鍓湰.js",
		"static/modules/manager/coretable/dbtable.html",
		"update/datas.groovy",
		"update/update_main.groovy",
		"view/manager/coretables/dbtable.html"
	]
	Context.getInstance().logService.info("script  delFile size[{}]",delFile.size())
	for(int i=0;i<delFile.size();i++) {
		try {
		def file=FileUitls.getClassRealPath(delFile[i]);
			Context.getInstance().logService.info("script delFile [{}]",file)
			FileUitls.deleteFile(file)
		}catch(Exception e){
			Context.getInstance().logService.info("script  delFile error:{}",e.toString())
		}
	}
	
	def deldir = [
			"view/manager/test",
			"static/libs/easyui",
			"static/debug",
			"static/modules/manager/datas",
			"static/modules/manager/functions",
			"static/modules/web/layuiadmin",
		]
	Context.getInstance().logService.info("script deldir size[{}]",deldir.size())
	for(int i=0;i<deldir.size();i++) {
		try {
			def dir=FileUitls.getClassRealPath(deldir[i]);
			Context.getInstance().logService.info("script deldir [{}]",dir)
			FileUitls.deleteDirectory(dir)
		}catch(Exception e){
			Context.getInstance().logService.info("script  deldir error:{}",e.toString())
		}
	}
}
start();